//
//  NetRequest.cpp
//  TestCpp
//
//  Created by Paladin on 13-3-11.
//
//

#include "NetRequest.h"

CNetRequest::CNetRequest(std::string requestTag)
{
    CCLOG("CNetRequest ctor, tag = %s", requestTag.c_str());
    mRequestTag = requestTag;
}

void CNetRequest::requestWithPost(const char* url, CPostData* postData, CCObject* object, SEL_CallFuncND func)
{
    // check parameter
    assert(url); assert(object); assert(func);
    
    CCHttpRequest* request = new CCHttpRequest();
    request->setUrl(url);
    request->setRequestType(CCHttpRequest::kHttpPost);
    request->setResponseCallback(object, func);
    // postData is a format of the Json string.
    // TODO::处理数据压缩和加密等处理(客户端发送的数据是否有必要压缩？？？cocos2dx中没有提供压缩的函数，只封装了解压zlib数据的函数)
    /*
        example::
        Json = {"data":postdata, "len":原始数据长度, "chksum":效验码, "encryp":true/false}
    */
    // write the post data
    request->setRequestData(postData->getPostData(), postData->getPostDataLen());
    request->setTag(mRequestTag.c_str());
    CCHttpClient::getInstance()->send(request);
    request->release();
}

void CNetRequest::requestWithGet(const char* url, CCObject* object, SEL_CallFuncND func)
{
    // check parameter
    assert(url); assert(object); assert(func);
    
    CCHttpRequest* request = new CCHttpRequest();
    // required fields
    request->setUrl(url);
    request->setRequestType(CCHttpRequest::kHttpGet);
    request->setResponseCallback(object, func);
    // optional fields
    request->setTag(mRequestTag.c_str());
    // send request
    CCHttpClient::getInstance()->send(request);
    // release it, pair to new
    request->release();
    return ;
}

int CNetRequest::getTimeoutForConnect()
{
    return CCHttpClient::getInstance()->getTimeoutForConnect();
}

void CNetRequest::setTimeoutForConnect(int value)
{
    CCHttpClient::getInstance()->setTimeoutForConnect(value);
}

void CNetRequest::encryption(void *data)
{
    
}
